﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace WebPortal.Template
{
    /// <summary>
    /// ssoLogin 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class ssoLogin : System.Web.Services.WebService
    {

        [WebMethod]
        public string GetEnUserId(string userId)
        {
            userId = EncryptTo(userId);
            return userId;
        }
        [WebMethod]
        public string GetDeUserId(string userId)
        {
            userId = DecryptTo(userId);
            return userId;
        }
        /// <summary>
        /// 对称加密函数 
        /// </summary>
        /// <param name="strText"></param>
        /// <returns></returns>
        public static string EncryptTo(string strText)
        {
            Byte[] byKey = { };
            Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes("&%#@?,:*".Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                string str = Convert.ToBase64String(ms.ToArray());
                str = HttpContext.Current.Server.UrlEncode(str);
                return str;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        /// <summary>
        /// 对称解密函数 
        /// </summary>
        /// <param name="strText"></param>
        /// <returns></returns>
        public static string DecryptTo(string strText)
        {
            //strText = HttpContext.Current.Server.UrlDecode(strText);
            Byte[] byKey = { };
            Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            Byte[] inputByteArray = new byte[strText.Length];
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes("&%#@?,:*".Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Convert.FromBase64String(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }


    }
}
